package grsu.epam.airline.service;

import grsu.epam.airline.DAO.IPlaneDAO;
import grsu.epam.airline.DAOImpl.DAOFactory;
import grsu.epam.airline.constant.Constant;
import grsu.epam.airline.model.Plane;

import java.sql.SQLException;
import java.util.ArrayList;

public class PlaneService {

	public void addPlane(String model) {
		Plane plane = new Plane(model);
		IPlaneDAO planeDao = (IPlaneDAO) DAOFactory.getInstance().getDAO(
				Constant.PlaneDAO);
		try {
			planeDao.addPlane(plane);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(planeDao.getConnection());
		}
	}

	public ArrayList<Plane> getAllPlane() {
		IPlaneDAO planeDao = (IPlaneDAO) DAOFactory.getInstance().getDAO(
				Constant.PlaneDAO);
		ArrayList<Plane> planes = null;
		try {
			planes = (ArrayList<Plane>) planeDao.getAllPlane();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(planeDao.getConnection());
		}
		return planes;
	}

	public void deletePlane(int planeId) {
		IPlaneDAO planeDao = (IPlaneDAO) DAOFactory.getInstance().getDAO(
				Constant.PlaneDAO);
		try {
			planeDao.deletePlane(planeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(planeDao.getConnection());
		}
	}

	public Plane getPlaneById(int planeId) {
		IPlaneDAO planeDao = (IPlaneDAO) DAOFactory.getInstance().getDAO(
				Constant.PlaneDAO);
		Plane plane = null;

		try {
			plane = (Plane) planeDao.getPlaneById(planeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(planeDao.getConnection());
		}
		return plane;
	}

	public void updatePlane(int planeId, String model) {
		IPlaneDAO planeDao = (IPlaneDAO) DAOFactory.getInstance().getDAO(
				Constant.PlaneDAO);

		try {
			planeDao.updatePlane(new Plane(planeId, model));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(planeDao.getConnection());
		}
	}
}
